﻿#include "mainwindow.h"
#include <QApplication>
#include <QMessageBox>
#include <QDebug>
#include "iostream"
#include <QRegularExpression>
#include <QRegularExpressionMatchIterator>
#include <QRegularExpressionMatch>
#define APPLICATION_NAME "格式转换"

bool startsWithChinesePrefix(QString *str) {
        // 检查字符串是否为空或null
        if (str->isEmpty()) {
            return false;
        }
        // 检查是否以“一”、“二”、“三”开头
        QString prefixes[] = {QStringLiteral("一"), QStringLiteral("二"), QStringLiteral("三"), QStringLiteral("四"),QStringLiteral("五"), QStringLiteral("六"), QStringLiteral("七"), QStringLiteral("八"), QStringLiteral("九"),QStringLiteral("十")};


        for (QString prefix : prefixes) {
            qDebug() << prefix;
            if (str->startsWith(prefix)) {
                return true;
            }
        }
        // 如果不是以这些中文字符开头，则返回false
        return false;
}

QList<QString> getSubjectNum(QString &text){
    QList<QString> contents;
    QRegularExpression regex("([0-9]+)\\.");
    QRegularExpressionMatchIterator it = regex.globalMatch(text);

    while (it.hasNext()) {
        QRegularExpressionMatch match = it.next();
        if (match.hasMatch()) {
//            QString firstChar = match.captured(0); // 获取匹配到的字符 56.
            contents.append(match.captured(1)); // 直接添加捕获的数字部分 56
            break; // 找到后退出循环
        }
    }
    return contents;
}

bool isAnswer(QString &text, QString &subType) {
    if(text == nullptr || text.isEmpty()){
        return false;
    }
    QRegularExpression regex(QStringLiteral("(?:（([A-Z]+)）$)|(?:\\(([A-Z]+)\\)$)"));
    if("3" == subType){
        regex.setPattern(QStringLiteral("(?:（([^）]+)）$)|(?:\\(([^)]+)\\)$)"));
    }
    QRegularExpressionMatch match = regex.match(text);
    return  match.hasMatch();
}

// 获取答案选项
QList<QString> getAnswerContents(QString &text, QString &subType) {
    QList<QString> contents;
    if(text == nullptr || text.isEmpty()){
        return contents;
    }
    QRegularExpression regex(QStringLiteral("(?:（([A-Z]+)）$)|(?:\\(([A-Z]+)\\)$)"));
    if("3" == subType){
        regex.setPattern(QStringLiteral("(?:（([^）]+)）$)|(?:\\(([^)]+)\\)$)"));
    }
    QRegularExpressionMatchIterator it = regex.globalMatch(text);
    while (it.hasNext()) {
        QRegularExpressionMatch match = it.next();
        if (match.hasMatch()) {
            int len = match.capturedLength();
            qDebug().noquote() << len << match.lastCapturedIndex();
            contents.append(match.captured(match.lastCapturedIndex())); // 直接添加捕获的数字部分 56
            break; // 找到后退出循环
        }
        //        if (match.captured(1)) {
        //            // 匹配到的是中文圆括号内的内容
        //            contents.append(match.captured(1));
        //        } else if (match.captured(2).isValid()) {
        //            // 匹配到的是英文圆括号内的内容
        //            contents.append(match.captured(2));
        //        }
    }
    return contents;
}


void test(){
    // 测试大题检测
    bool bl;
    QString text = QStringLiteral("一、单选题");
    bl = startsWithChinesePrefix(&text);
    if(bl){
        qDebug() << text<< "bl is true";
    } else {
        qDebug() << text << "bl is false";
    }

    // 匹配题目号
    QString text2 = QStringLiteral("56. 脚手架在土木建筑工程1.2中的作用是什么（C）");
    QList<QString> list = getSubjectNum(text2);
    qDebug() << text2 << list.size();

    // 匹配答案
    QString text3 = QStringLiteral("56. 脚手架在土木建筑工程1.2中的作用是什么（C）");
    QString subjectType = "1";
    bl = isAnswer(text3, subjectType);
    if(bl){
        qDebug().noquote() << getAnswerContents(text3,subjectType) << "bl is true";
    } else {
        qDebug().noquote() << getAnswerContents(text3,subjectType) << "bl is false";
    }


    // 匹配答案
    QString text4 = QStringLiteral("1000.急停按钮的电路可以与其他控制电路合并(×)");
    subjectType = "3";
    bl = isAnswer(text4, subjectType);
    if(bl){
        qDebug().noquote() << getAnswerContents(text4,subjectType) << "bl is true";
    } else {
        qDebug().noquote() << getAnswerContents(text4,subjectType) << "bl is false";
    }
}



int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QString strApplicationName = QStringLiteral(APPLICATION_NAME);
    QApplication::setApplicationName(strApplicationName);
    QIcon appIcon(":/logo/logo.ico"); // 使用资源文件中的logo图像
    a.setWindowIcon(appIcon);
    a.setOrganizationDomain(QStringLiteral("武汉芯硕科技有限公司"));
    a.setOrganizationName(QStringLiteral("武汉芯硕科技有限公司"));
    a.setApplicationVersion("1.0.1");
    MainWindow w;
    w.setWindowIcon(appIcon);
    w.setWindowTitle(strApplicationName); // 设置窗口标题
    w.show();

    // 测试
//    test();

    return a.exec();
}
